// pages/pingjiaDianpu/pingjiaDianpu.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    niMing: false,
    xiaoshouX: 4,
    shouxinX: 4,
    textval: '',
    tupianList: [],
    shopid: '',
    shopname: '',
    goodsid: '',
    shopimg: '',
    dingdanid: ''
  },
  addImg() {
    let that = this
    let oldtupianList = that.data.tupianList
    let num = 4 - oldtupianList.length
    wx.chooseImage({
      count: num,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        setTimeout(function() {
          wx.showLoading({
            title: '上传中',
            mask: true
          })
        }, 0)
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        let list = []
        for (let i in tempFilePaths) {
          list.push(wx.getFileSystemManager().readFileSync(tempFilePaths[i], "base64"))
        }
        console.log(list)
        wx.request({
          url: app.globalData.myurl,
          method: 'POST',
          data: {
            cmd: "uploadPluralImg",
            imgList: JSON.stringify(list),
          },
          header: {
            "content-type": "application/x-www-form-urlencoded",
            "token": wx.getStorageSync('token')
          },
          success(res) {
            if (res.data.zjstate == 1) {
              setTimeout(function () {
                wx.hideLoading()
                wx.stopPullDownRefresh() //停止下拉刷新
              }, 0)
              setTimeout(function () {
                wx.showModal({
                  title: '您的账号已在其他设备登录，请重新登录！',
                  showCancel: false, //是否显示取消按钮false去掉取消按钮
                  success: function (res) {
                    if (res.confirm) {
                      wx.clearStorageSync()
                      wx.reLaunch({
                        url: '/pages/login/login',
                      })
                    } else {
                      wx.clearStorageSync()
                      wx.reLaunch({
                        url: '/pages/login/login',
                      })
                    }
                  }
                })
              }, 0)
              return
            }
            console.log(res)
            if (res.data.result == 0) {
              for (let j in res.data.imgUrllist) {
                oldtupianList.push(res.data.imgUrllist[j])
              }
              that.setData({
                tupianList: oldtupianList
              })
            }
            setTimeout(function() {
              wx.hideLoading()
            }, 0)
            setTimeout(function() {
              wx.showToast({
                title: res.data.resultNote,
                icon: 'none',
                duration: 1500
              })
            }, 0)
          }
        });
      }

    })

  },
  shanchu(e) {
    let indexs = e.currentTarget.dataset.index
    let tupianList = this.data.tupianList
    tupianList.splice(indexs, 1)
    this.setData({
      tupianList: tupianList
    })
  },
  getpingjia(e) {
    let that = this
    console.log(e.detail.value)
    that.setData({
      textval: e.detail.value
    })
  },
  //销售评价星星
  xingxingp1(e) {
    let that = this
    let xiaoshouX = that.data.xiaoshouX
    let num = e.currentTarget.dataset.num
    let type = e.currentTarget.dataset.type
    if (type == 'an') {
      console.log('55555')
      xiaoshouX = xiaoshouX + num + 1
    } else {
      xiaoshouX = num + 1
    }
    that.setData({
      xiaoshouX: xiaoshouX
    })
  },
  //守信评价星星
  xingxingp2(e) {
    let that = this
    let xiaoshouX = that.data.shouxinX
    let num = e.currentTarget.dataset.num
    let type = e.currentTarget.dataset.type
    if (type == 'an') {
      console.log('55555')
      xiaoshouX = xiaoshouX + num + 1
    } else {
      xiaoshouX = num + 1
    }
    that.setData({
      shouxinX: xiaoshouX
    })
  },
  // 是否匿名
  setniMing() {
    this.setData({
      niMing: !this.data.niMing
    })
  },
  // 发布
  faBu() {
    let that = this
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "toComment",
        shopId: wx.getStorageSync('shopId'),
        userId: wx.getStorageSync('userId'),
        goodsId: that.data.goodsid,
        reshopId: that.data.shopid,
        desc: that.data.textval,
        star: that.data.xiaoshouX,
        integrity: that.data.shouxinX,
        imgList: JSON.stringify(that.data.tupianList),
        orderId: that.data.dingdanid
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        setTimeout(function() {
          wx.hideLoading()
        }, 0)
        setTimeout(function() {
          wx.showToast({
            title: res.data.resultNote,
            icon: 'none',
            duration: 1500
          })
        }, 0)
        console.log(res)
        if (res.data.result == 0) {
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.setData({
      shopid: options.shopid,
      shopname: options.shopname,
      goodsid: options.goodsid,
      shopimg: options.shopimg,
      dingdanid: options.dingdanid,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})